﻿//namespace OppoAds 
//{
using UnityEngine;
//}

public class AndroidNativeManager : INativeManager
{

    protected readonly AndroidJavaObject nativeManager;

    /// <summary>
    /// Gets the unity main activity.
    /// </summary>
    internal static AndroidJavaObject GetActivity()
    {
        if (s_activity == null)
        {
            var unityPlayer = new AndroidJavaClass(
                "com.unity3d.player.UnityPlayer");
            s_activity = unityPlayer.GetStatic<AndroidJavaObject>(
                "currentActivity");
        }

        return s_activity;
    }

    private static AndroidJavaObject s_activity;
    AndroidJavaObject activity => GetActivity();

    /// <summary>
    /// Initializes a new instance of the <see cref="NativeAd"/> class.
    /// </summary>
    public AndroidNativeManager()
    {
        var jc = new AndroidJavaClass("com.feiya.android.NativeManager");
        AndroidJavaObject manager = jc.CallStatic<AndroidJavaObject>("getNativeManager");
        this.nativeManager = manager;
    }

    public void RequestFrequency()
    {
        CallMethod("requestFrequency");
    }

    void CallMethod(string methodName)
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        if (activity != null)
        {
            Debug.Log("methodName:"+methodName);

            var runnable = new AndroidJavaRunnable(
                () => this.nativeManager.Call(methodName, activity));
            activity.Call("runOnUiThread", runnable);
        }
#endif
    }

}

//}